<?php
Namespace Cyfy\Modules\Forms;
use \Cyfy\Message;
use \Cyfy\Modules\Forms\Module;

/**
 * Radio class.
 *
 * @author Dandelion <dandelion8888@gmail.com>
 * @version 0.1
 */
class Radio extends Input
{
    /**
     * Array with options in associative array key => value.
     * @var array
     */
    public $options = array();

    /**
     * Creates a radio element
     *
     * @param string Radio name
     * @param string Radio title
     * @param array Options in associative array.
     * @param string Default value - will be checked
     * @param bool Required
     */
    function __construct($name, $title, $options=array(), $value = null, $required=false)
    {
        $this -> name = $name;
        $this -> title = $title;
        $this -> values = $values;
        $this -> required = $required;
        $this -> options = $options;
        $this -> value = $value;
    }
    
    /**
     * Returns a string containing the radio.
     * @return string HTML content
     */
    function __toString()
    {
        $this -> loadDescription();        
        $required = ($this->required) ? " form-validate" : "";
        $star = ($this->required) ? Module :: REQUIRED_CHAR : "";
        $output = "<div class=\"form-item form-item-{$this->name}\"> \n";
        $output .= "    <label class=\"form-label-{$this->name}{$required}\" title=\"{$this->description}\" for=\"{$this->name}\">".$this -> title.": {$star}</label> \n";
        foreach($this -> options as $val => $title)
        {
            $checked = ($this->value == $val) ? "checked=\"checked\"" : "";
            $output .= "	<div class=\"form-radio\"><span class=\"form-radio-label\">{$title}</span><input type=\"radio\" class=\"form-input-{$this->name}\" name=\"{$this->name}\" value=\"{$val}\" {$checked}/></div>";
        }
        $output .= "</div> \n\n";
        return $output;
    }
     
    /** 
     * Validates the data.
     * @return bool
     */
    function validate()
    {
        // required
        if($this->required){
            if($this -> value == ""){
                Message :: set(\Cyfy\Modules\Forms::t(121, array($this -> title)), Message::ERROR);
                return false;
            }
        }
        return true;
    }
}